package crew.gwt.client.widgets.util;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.ui.Widget;

import crew.gwt.client.domain.PersistableEntity;


public abstract class EntityGridFeature<W extends Widget,T extends PersistableEntity<T>> {

	private String title;
	private Widget header;
	private Widget settingsPl;
	
	/**
	 * Used to keep the UpdatableEntityWidget reference of a Entity
	 */
	private final Map<T,UpdatableEntityWidget<W,T>> widgetOfEntity = new HashMap<T,UpdatableEntityWidget<W,T>>();
	
	/**
	 * Create the UpdatableEntityWidget which is the representation of entityOfWidget
	 * @param entityOfWidget ,entity of the displayed row
	 * @return the widget displayed in the feature cell
	 */
	protected abstract UpdatableEntityWidget<W,T> createUpdatableEntityWidget(T entityOfWidget);

	/**
	 * Get or create the widget related of the entity
	 * @param entityOfWidget the entity reference
	 * @return the widget found, or a new one if they are not
	 */
	protected UpdatableEntityWidget<W,T> getEnityRowWidget(T entityOfWidget){
		// Widget is already created ?
		if(getWidgetOfEntity().containsKey(entityOfWidget)){
			return getWidgetOfEntity().get(entityOfWidget);
		}
		// if no, create
		UpdatableEntityWidget<W,T> widget = createUpdatableEntityWidget(entityOfWidget);
		widget.getWidget().addStyleName("EntityGrid-Feature-Element");
		getWidgetOfEntity().put(entityOfWidget, widget);
		return widget;
	}
	
	public Map<T, UpdatableEntityWidget<W,T>> getWidgetOfEntity() {
		return widgetOfEntity;
	}
	
	public Widget getHeader() {
		return header;
	}

	public Widget getSettingsPl() {
		return settingsPl;
	}

	public void setHeader(Widget header) {
		this.header = header;
		this.header.addStyleName("EntityGrid-Feature-Header");
	}

	public void setSettingsPl(Widget settingsPl) {
		this.settingsPl = settingsPl;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
